package com.e1xp.deport_management_system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.e1xp.deport_management_system.entity.OrderProduct;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author E1xP
 * @since 2021-04-09
 */
public interface OrderProductMapper extends BaseMapper<OrderProduct> {

    @Insert("<script>" +
            "INSERT INTO order_product (order_id,product_id,count,price,stats,instance_id) VALUES " +
            "<foreach item='item' collection='orderProductList' separator=','> " +
            "(#{item.orderId},#{item.productId},#{item.count},#{item.price},#{item.stats},#{item.instanceId})" +
            "</foreach>" +
            "</script>")
    void addList(@Param("orderProductList") List<OrderProduct> orderProductList);

    @Select("SELECT * FROM order_product where instance_id=#{instanceId} and order_id=#{orderId} and stats=1")
    List<OrderProduct> getUnpick(@Param("instanceId") long instanceId, @Param("orderId") long orderId);

    @Update("<script>" +
            "UPDATE order_product SET " +
            "pick_count = case id " +
            "<foreach item='item' collection='orderProductList' index='index'>" +
            "WHEN #{item.id} THEN #{item.pickCount} " +
            "</foreach>" +
            "END, " +
            "stats = CASE id" +
            "<foreach item='item' collection='orderProductList' index='index'>" +
            "WHEN #{item.id} THEN #{item.stats}" +
            "</foreach>" +
            "END " +
            "WHERE id in " +
            "<foreach item='item' collection='orderProductList' index='index' separator=',' open='(' close=')'>" +
            "#{item.id}" +
            "</foreach>" +
            "</script>")
    void updateList(@Param("orderProductList") List<OrderProduct> orderProductList);
}
